package cn.aixuxi.security.config;

import cn.aixuxi.security.service.impl.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/css/**","/images/**","/fonts/**")
                .permitAll()
                .anyRequest().authenticated()
//                .anyRequest().authenticated()
                .and()

                .formLogin()
                .loginPage("/login.ftl")
                .loginProcessingUrl("/login")
                .defaultSuccessUrl("/index")
                .permitAll()
//                .authorizeRequests()
//                .antMatchers("/login*").permitAll()
//                .and().authorizeRequests()
//                .antMatchers("/*").hasAuthority("test:view")
                .and()
                .csrf().disable()
                .logout().logoutUrl("/logout");
//                .addLogoutHandler(new TokenLogoutHandler(tokenManager,redisUtils))
//                .and()
//                .addFilter(new TokenLoginFilter(tokenManager,redisUtils,authenticationManager()))
//                .addFilter(new TokenAutheFilter(authenticationManager(),tokenManager,redisUtils))
//                .httpBasic();
      /*  http.authorizeRequests()
                .antMatchers("/static/**") //静态资源放行
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .loginPage("/login.ftl")//自定义登录页面
                .loginProcessingUrl("/login")
                .loginProcessingUrl("/index")
                .and()
                .csrf().disable()
                .logout().logoutUrl("/logout");*/

    }

    /**
     * 功能描述: 自定义UserDetailsService及密码加密
     * @param auth 1
     * @author : ruozhuliufeng
     * @date : 2021/4/22 20:42
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    /**
     * 功能描述: 密码加密
     * @return : org.springframework.security.crypto.password.PasswordEncoder
     * @author : ruozhuliufeng
     * @date : 2021/4/22 20:42
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder(12);
    }
}
